package com.abe.mvpcore;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class PresenterHelper {

    private List<BasePresenter> mPresenterRefs;
    private List<String> mPresenterNames;
    private List<String> mViewNames;

    private static PresenterHelper INSTANCE;

    private PresenterHelper() {
        mPresenterRefs = new ArrayList<>();
        mPresenterNames = new ArrayList<>();
        mViewNames = new ArrayList<>();
    }

    public static PresenterHelper getInstance() {
        if (INSTANCE == null)
            INSTANCE = new PresenterHelper();
        return INSTANCE;
    }

    public Object getPresenter(Class<?> presenterClass, BaseView view) {
        if (!checkNotNull()) {
            mPresenterRefs = new ArrayList<>();
            mPresenterNames = new ArrayList<>();
            mViewNames = new ArrayList<>();
        }
        //检查是否存在引用
        String presenterName = presenterClass.getSimpleName();
        String viewName = view.getClass().getSimpleName();
        Object presenter = check(viewName, presenterName);
        //实例化
        if (presenter == null) {
            try {
                //获得实例
                presenter = presenterClass.newInstance();
                //绑定View
                ((BasePresenter) presenter).attachView(view);
                mPresenterRefs.add((BasePresenter) presenter);
                mPresenterNames.add(presenterName);
                mViewNames.add(viewName);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        //printLog();
        //返回实例
        return presenter;
    }

    private void printLog() {
        w(mViewNames.toString());
        w(mPresenterNames.toString());
        w(String.valueOf(mPresenterRefs.size()));
    }

    private void w(String m) {
        System.out.println(m);
    }

    private Boolean checkNotNull() {
        return mViewNames != null && mPresenterNames != null && mPresenterRefs != null;
    }

    private Object check(String viewName, String presenterName) {
        if (TextUtils.isEmpty(viewName) || TextUtils.isEmpty(presenterName)) return null;
        Object obj = null;
        if (checkNotNull()) {
            int k = -1;
            for (int i = 0; i < mViewNames.size(); i++) {
                String presenter = mPresenterNames.get(i);
                String view = mViewNames.get(i);
                if (presenter.equals(presenterName) && view.equals(viewName)) {
                    if (mPresenterRefs.get(i) != null) {
                        obj = mPresenterRefs.get(i);
                    } else {
                        k = i;
                    }
                    break;
                }
            }
            if (k != -1) {
                mPresenterRefs.remove(k);
                mViewNames.remove(k);
                mPresenterNames.remove(k);
            }
        }
        return obj;
    }

    /**
     * 清理当前活动或者碎片中presenter
     */
    public void destroy(BaseView view) {
        String viewName = view.getClass().getSimpleName();
        //w(viewName);
        if (checkNotNull()) {
            List<Integer> pos = new ArrayList<>();
            for (int i = 0; i < mViewNames.size(); i++) {
                if (mViewNames.get(i).equals(viewName)) {
                    pos.add(i);
                }
            }
            if (pos.size() > 0) {
                for (int i = pos.size() - 1; i >= 0; i--) {
                    int k = pos.get(i);
                    if (mPresenterRefs.get(k) != null) {
                        mPresenterRefs.get(k).detachView();
                    }
                    mPresenterRefs.remove(k);
                    mViewNames.remove(k);
                    mPresenterNames.remove(k);
                }
            }
        }
        //printLog();
    }

    /**
     * 清理全部
     */
    public void destroyAll() {
        if (checkNotNull()) {
            for (BasePresenter presenter : mPresenterRefs) {
                if (presenter != null) {
                    presenter.detachView();
                }
            }
            mPresenterRefs.clear();
            mPresenterNames.clear();
            mViewNames.clear();
        }
        //printLog();
    }
}
